import React, { Suspense } from 'react';
import type { RouteObject } from 'react-router-dom';

export default interface MenuType{
    icon:string,
    key:string,
    label:string,
    children?:MenuType[]
}

// 定义路由对照表（使用懒加载）
export const routesList :any= {
    '/dashboard': React.lazy(() => import('../page/dashborad')),
    '/users/list': React.lazy(() => import('../page/tenantsmanage/tenantsList')),
    '/users/add': React.lazy(() => import('../page/tenantsmanage/addtenant')),
    '/estate/car': React.lazy(() => import('../page/eatate/carmanage')),
    '/estate/room': React.lazy(() => import('../page/eatate/roommanage')),
    '/estate/tenement': React.lazy(() => import('../page/eatate/tenement')),
    '/repair': React.lazy(() => import('../page/repair')),
    '/finance/bill': React.lazy(() => import('../page/finance/billManage')),
    '/finance/contract': React.lazy(() => import('../page/finance/contractManage')),
    '/finance/surrender': React.lazy(() => import('../page/finance/contractDetail')),
    '/merchants': React.lazy(() => import('../page/InvestmentManage')),
    '/operation/all': React.lazy(() => import('../page/operationManage/operationView')),
    '/operation/article': React.lazy(() => import('../page/operationManage/articalPublish')),
    '/operation/comments': React.lazy(() => import('../page/operationManage/content')),
    '/equipment': React.lazy(() => import('../page/equipmentManage')),
    '/energy': React.lazy(() => import('../page/energyManage')),
    '/settings': React.lazy(() => import('../page/systemSetting')),
    '/personal': React.lazy(() => import('../page/personalCenter'))
};

export function generatesRouters(menu:MenuType[]){
    return menu.map((item:MenuType)=>{
        // 获取路由组件
        const Component = routesList[item.key];
        // 创建路由元素，确保正确包装懒加载组件
        let routeElement = null;
        if (Component) {
            routeElement = (
                <Suspense fallback={<div>Loading...</div>}>
                    <Component />
                </Suspense>
            );
        }
        
        const routerObj:RouteObject={
            path: item.key,
            element: routeElement,
        }
        
        if(item.children){
            routerObj.children=generatesRouters(item.children)
        }
        return routerObj;
    })
}